//
//  SRSetting.m
//  SmartRebate
//
//  Created by Yuan on 13-2-28.
//  Copyright (c) 2013年 unionpaysmart.com. All rights reserved.
//

#import "SRSetting.h"
#import "SFHFKeychainUtils.h"

NSString * const ThemeDidChangeNotification = @"me.smartrebate.theme.change";

NSString * const SelfTheme = @"me.smartrebate.theme";

#define kThemeDefault   @"default"

static SRSetting *setting = nil;

@implementation SRSetting

+(SRSetting *)sharedSetting
{
    @synchronized(self)
    {
        if (setting == nil)
        {
            setting = [[SRSetting alloc]init];
        }
        return setting;
    }
    return nil;
}


#pragma 保存账号密码
-(void)saveAccount:(NSString *)account password:(NSString *)pwd
{
    if ([SFHFKeychainUtils storeUsername:account andPassword:pwd forServiceName:WEBSERVICE_URL updateExisting:true error:nil])
    {        
        HYLog(@"保存账号密码成功！");
    }
    else
    {
        HYLog(@"保存账号密码失败！");
    };
}

#pragma 删除账号密码
-(void)deletePasswordForAccount:(NSString *)account
{
    if ([SFHFKeychainUtils deleteItemForUsername:account andServiceName:WEBSERVICE_URL error:nil])
    {
        HYLog(@"删除账号密码成功！");
    }
    else
    {
        HYLog(@"删除账号密码失败！");
    }
}

-(void)storeTheme:(NSString *)theme
{
    if (theme)
    {
        NSUserDefaults *def = [NSUserDefaults standardUserDefaults];
        [def setValue:theme forKey:SelfTheme];
        [def synchronize];
        [[NSNotificationCenter defaultCenter]
         postNotificationName:ThemeDidChangeNotification
         object:nil];
    }
}

-(NSString *)getTheme
{
    NSUserDefaults *def = [NSUserDefaults standardUserDefaults];
    
    if ([def objectForKey:SelfTheme])
    {
        return [def objectForKey:SelfTheme];
    }
    return kThemeDefault;
}

- (UIImage *)imageWithImageName:(NSString *)imageName
{
    NSString *directory = [NSString stringWithFormat:@"/%@/%@", @"style",[self getTheme]];
    NSString *imagePath = [[NSBundle mainBundle] pathForResource:imageName
                                                          ofType:@"png"
                                                     inDirectory:directory];
    return [UIImage imageWithContentsOfFile:imagePath];
}

@end














